package com.qiezikaka.kaka.ui.detail;

import android.app.Activity;

import com.qiezikaka.kaka.base.BasePresenter;
import com.qiezikaka.kaka.bean.SiteDetailInfo;
import com.qiezikaka.kaka.bean.SiteSceneShowInfo;
import com.qiezikaka.kaka.bean.UploadImgInfo;
import com.qiezikaka.kaka.net.DataManager;
import com.qiezikaka.kaka.ui.detail.DetailContract;

import java.io.File;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Subscription;
import rx.functions.Action1;

public class DetailPresenter extends BasePresenter<DetailContract.View> implements DetailContract.Presenter {

    public DetailPresenter(Activity activity, DetailContract.View view) {
        super(activity, view);
    }

    @Override
    public void getSiteDetail(String pid) {
        Subscription subscribe = DataManager.getInstance().getSiteDetail(pid)
                .subscribe(new Action1<SiteDetailInfo>() {
                    @Override
                    public void call(SiteDetailInfo InfoData) {
                        if (InfoData != null) {
                            mView.onSiteDetail(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }

    /**
     * 风格详情图
     * @param id
     */
    @Override
    public void getSiteSceneShow(String id) {
        Subscription subscribe = DataManager.getInstance().getSiteSceneShow(id)
                .subscribe(new Action1<SiteSceneShowInfo>() {
                    @Override
                    public void call(SiteSceneShowInfo InfoData) {
                        if (InfoData != null) {
                            mView.onSiteSceneShow(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }

    @Override
    public void onLoadImg(RequestBody pid, MultipartBody.Part thumb) {
        Subscription subscribe = DataManager.getInstance().onLoadImg(pid,thumb)
                .subscribe(new Action1<UploadImgInfo>() {
                    @Override
                    public void call(UploadImgInfo InfoData) {
                        if (InfoData != null) {
                            mView.onUpload(InfoData);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                        throwable.printStackTrace();
                        mView.onFailer(throwable);
                    }
                });
        addSubscribe(subscribe);
    }
}
